#include "../head/workplatform.h"
#include "ui_workplatform.h"
#include <QtCore/QDir>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include <QAxObject>
#include "../head/utils/exportutil.h"
#include "../head/mainwindow.h"


WorkPlatForm::WorkPlatForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WorkPlatForm)
{
    ui->setupUi(this);
    //初始化散点图
    series1 = new QScatterSeries();              // 创建一个散点数据集对象
    series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series1->setMarkerSize(10);
    series1->setName("俯视图设计曲线");
    series5 = new QScatterSeries();              // 创建一个散点数据集对象
    series5->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series5->setMarkerSize(10);
    series5->setName("俯视图施工曲线");
    chart1 = new QChart();
    axisX1 = new QValueAxis();
    axisY1 = new QValueAxis();
    axisX1->setMin(0);
    axisY1->setMin(-20);
    axisX1->setMax(160);
    axisY1->setMax(20);
    axisY1->setReverse(true);                      //坐标翻转
    axisX1->setLabelFormat("%d");
    axisY1->setLabelFormat("%0.1f");
    chart1->addAxis(axisY1, Qt::AlignLeft);                      // 将X轴添加到图表上
    chart1->addAxis(axisX1, Qt::AlignTop);                    // 将Y轴添加到图表上
    chart1->legend()->setVisible(true);               // 隐藏图例
 //  m_axisX->setTitleText("X-label");
 //  m_axisY->setTitleText("Y-label");
    chart1->addSeries(series1);                              // 将曲线对象添加到图表上
    series1->attachAxis(axisX1);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series1->attachAxis(axisY1);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
    chart1->addSeries(series5);                              // 将曲线对象添加到图表上
    series5->attachAxis(axisX1);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series5->attachAxis(axisY1);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
    chartView1 = new QChartView(chart1);
    //chartView1->chart()->setTitle("设计曲线与施工曲线");
    ui->gridLayoutLine_2->addWidget(chartView1, 1, 1);



    //初始化散点图
    series6 = new QScatterSeries();              // 创建一个散点数据集对象
    series6->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series6->setMarkerSize(10);
    series6->setName("侧视图设计曲线");
    series7 = new QScatterSeries();              // 创建一个散点数据集对象
    series7->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
    series7->setMarkerSize(10);
    series7->setName("侧视图施工曲线");
    chart4 = new QChart();
    axisX2 = new QValueAxis();
    axisY2 = new QValueAxis();
    axisX2->setMin(0);
    axisY2->setMin(0);
    axisX2->setMax(160);
    axisY2->setMax(20);
    axisY2->setReverse(true);                      //坐标翻转
    axisX2->setLabelFormat("%d");
    axisY2->setLabelFormat("%0.1f");
    chart4->addAxis(axisY2, Qt::AlignLeft);                      // 将X轴添加到图表上
    chart4->addAxis(axisX2, Qt::AlignTop);                    // 将Y轴添加到图表上
    chart4->legend()->setVisible(true);               // 隐藏图例
 //  m_axisX->setTitleText("X-label");
 //  m_axisY->setTitleText("Y-label");
    chart4->addSeries(series6);                              // 将曲线对象添加到图表上
    series6->attachAxis(axisX2);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series6->attachAxis(axisY2);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
    chart4->addSeries(series7);                              // 将曲线对象添加到图表上
    series7->attachAxis(axisX2);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
    series7->attachAxis(axisY2);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
    chartView2 = new QChartView(chart4);
    //chartView2->chart()->setTitle("设计曲线与施工曲线");
    ui->gridLayoutLine_1->addWidget(chartView2, 1, 1);




    //初始化雷达图
    series2 = new QSplineSeries();
    series2->append(0,0);

    chart2 = new QPolarChart();
    chart2->legend()->setVisible(false);

  //  chart3->legend()->setAlignment(Qt::AlignLeft);// 隐藏图例
    angularAxis2 = new QValueAxis();
    //第一个和最后一个间隔位于0/360度角
    angularAxis2->setTickCount(25);
    angularAxis2->setMinorTickCount(15);
    //angularAxis->setGridLineVisible(false);           //大刻度是否可见
    angularAxis2->setMinorGridLineVisible(false);        //小刻度是否可见
    angularAxis2->setLabelFormat("%d");
    //设置角度坐标系的范围是0~360度
    angularAxis2->setRange(0,360);
    //将坐标轴加入到图形中去，注意第二个参数的选择
    chart2->addAxis(angularAxis2,QPolarChart::PolarOrientationAngular);
    //设置弧长的坐标系设置
    radialAxis2 = new QValueAxis();
    radialAxis2->setTickCount(5);
    radialAxis2->setLabelsVisible(false);
    //radialAxis->setLabelFormat("%ddB");
    radialAxis2->setRange(0,10);
    radialAxis2->setLineVisible(false);          //轴线是否可见
    //将弧长坐标系加入到图形中去
    chart2->addAxis(radialAxis2,QPolarChart::PolarOrientationRadial);
    charview2 = new QChartView(chart2);
    charview2->setChart(chart2);
    chart2->addSeries(series2);


    series2->attachAxis(angularAxis2);
    series2->attachAxis(radialAxis2);
    series2->append(0,10);
    series2->setPen(QPen(Qt::red,3));


    charview2->setRenderHint(QPainter::Antialiasing);   //抗锯齿
    ui->gridLayout_7->addWidget(charview2,1,1);

    series3 = new QSplineSeries();
    series3->append(0,0);
    series4 = new QSplineSeries();
    series4->append(0,0);
    seriesCircle = new QSplineSeries();

    chart3 = new QPolarChart();
    chart3->legend()->setVisible(false);

  //  chart3->legend()->setAlignment(Qt::AlignLeft);// 隐藏图例
    angularAxis3 = new QValueAxis();
    //第一个和最后一个间隔位于0/360度角
    angularAxis3->setTickCount(25);
    angularAxis3->setMinorTickCount(15);
    //angularAxis->setGridLineVisible(false);           //大刻度是否可见
    angularAxis3->setMinorGridLineVisible(false);        //小刻度是否可见
    angularAxis3->setLabelFormat("%d");
    //设置角度坐标系的范围是0~360度
    angularAxis3->setRange(0,360);
    //将坐标轴加入到图形中去，注意第二个参数的选择
    chart3->addAxis(angularAxis3,QPolarChart::PolarOrientationAngular);
    //设置弧长的坐标系设置
    radialAxis3 = new QValueAxis();
    radialAxis3->setTickCount(5);
    radialAxis3->setLabelsVisible(false);
    //radialAxis->setLabelFormat("%ddB");
    radialAxis3->setRange(0,10);
    radialAxis3->setLineVisible(false);          //轴线是否可见
    //将弧长坐标系加入到图形中去
    chart3->addAxis(radialAxis3,QPolarChart::PolarOrientationRadial);
    charview3 = new QChartView(chart3);
    charview3->setChart(chart3);
    chart3->addSeries(series3);
    chart3->addSeries(series4);
    //将曲线和坐标轴绑定起来

    series3->attachAxis(angularAxis3);
    series3->attachAxis(radialAxis3);    
    series4->attachAxis(angularAxis3);
    series4->attachAxis(radialAxis3);

    series3->append(0,10);
    series4->append(0,10);


    series3->setPen(QPen(Qt::blue,3));
    series4->setPen(QPen(Qt::green,3));
    //charview->chart()->setTitle("雷达图");
   // charview3->chart()->setTitle("方位角与倾角");

    charview3->setStyleSheet("background: transparent");


    charview3->setRenderHint(QPainter::Antialiasing);   //抗锯齿
    ui->gridLayout_5->addWidget(charview3,1,1);

    //connect(demoduleUtil, &DemoduleUtil::sendDecCode2WorkPlat, this,&WorkPlatForm::staticAndDynamicData);
    read_Excel();

}

void WorkPlatForm::initParam(){
}


/****AA AA 01 20 03 9F 0A 2C 26 13 1A 2C 00 88
 * 20 03 倾角 INC 低八位与上高八位除以10 ，减去90； (5位 4位)
 * 9F 0A 方位角 AZi 低八位与上高八位除以10     (7位6位)
 * 2C 26 重力和 GT 低八位与上高八除以10000 (9位，8位)
 * 13 工具面角 TF 直接乘2 (10位)
 * 1A 温度 T
 * 2C 场强  MAG
 * 00 ? 磁偏角
 * 88 电压 BTT 直接除以10
 * **********************************************/
void WorkPlatForm::staticAndDynamicData(QByteArray decData){
    double data1=0;


    if(decData.size() == 14){ //静态数据
        series2->remove(1);
        series3->remove(1);
        series4->remove(1);

        data1 = ((double)((uint8_t)decData[4]*256 + (uint8_t)decData[3])/10)-90; //倾角
        series3->append(data1,10);
        ui->label_INC->setText(QString::number(data1,'f',1));

        data1 = (double)((uint8_t)decData[6]*256+(uint8_t)decData[5])/10; //方位角
        series4->append(data1,10);
        ui->label_AZI->setText(QString::number(data1,'f',1));

        data1 = (double)((uint8_t)decData[9])*2; //工具面角
        series2->append(data1,10);
        ui->label_TF->setText(QString::number(data1,'f',1));


        data1 = (double)((uint8_t)decData[8]*256+(uint8_t)decData[7])/10000; //重力和
        ui->label_GT->setText(QString::number(data1,'f',4));

        ui->label_TEMP->setText(QString::number((uint8_t)decData[10],'f',1)); //温度

        ui->label_BT->setText(QString::number((uint8_t)decData[11],'f',1)); //场强

        ui->label_DIP->setText(QString::number((uint8_t)decData[12],'f',1)); //磁偏角


        data1 = (double)((uint8_t)decData[13])/10; //电压
        ui->label_BATV->setText(QString::number(data1,'f',1));
    }else if(decData.size() == 5){ //动态数据
        series2->remove(1);

        data1 = (double)((uint8_t)decData[3])*2; //工具面角
        series2->append(data1,10);
        ui->label_TF->setText(QString::number(data1,'f',1));
    }


}


void WorkPlatForm::renderData(double _CourseAngle_fy,double _PitchAngle_fy,double _RollAngle_fy)
{    

    //方位角(航向角)
    CourseAngle_fy  = _CourseAngle_fy;
    //倾角(俯仰角)
    PitchAngle_fy =  _PitchAngle_fy;
    //工具面角
    RollAngle_fy = _RollAngle_fy;
    //ui->ssfwj->setText(QString::number(CourseAngle_fy));
    //ui->ssqj->setText(QString::number(PitchAngle_fy));
    //ui->ssgzmxj->setText(QString::number(RollAngle_fy));
    series2->remove(1);
    series3->remove(1);
    series4->remove(1);
    drawGJMPieChart();
    drawFwjQjPieChart();
    figureDrillPosition(CourseAngle_fy,PitchAngle_fy,DrillLength);
}

void WorkPlatForm::QAM_Demodulator()
{
    return;
}


//绘制工具面图
void WorkPlatForm::drawGJMPieChart(){

    // 向series中添加数据，X：角度 Y：到圆心距离
   //series2->append(RollAngle_fy*180/PI,10);
   series2->append(RollAngle_fy,10);
}

//绘制方位角倾角
void WorkPlatForm::drawFwjQjPieChart()
{

    // 向series中添加数据，X：角度 Y：到圆心距离
    //series3->append(CourseAngle_fy*180/PI,10);
    series3->append(CourseAngle_fy,10);
    // 向series中添加数据，X：角度 Y：到圆心距离
    //series4->append(PitchAngle_fy*180/PI,10);
    series4->append(PitchAngle_fy,10);
}


void WorkPlatForm::figureDrillPosition (double courseAngle, double pitchAngle, double drillLength) { //courseAngle 航向角， pitchAngle 俯仰角，drillLength 钻杆长度

        x_2 = x_2 + drillLength * sin(pitchAngle) * sin(courseAngle);
        y_2 = y_2 + drillLength * sin(courseAngle) * cos(pitchAngle);
        z_2 = z_2 + drillLength * cos(pitchAngle);
        series5->append(x_2,y_2);
        series7->append(x_2,z_2);
}

double WorkPlatForm::radian2angle(int radian){
    double angle = PI/(radian/180);
    qDebug()<< "radian=:angle = :" << radian << angle <<endl;
    return angle;
}

void WorkPlatForm::figureDrillPosition_1 (double courseAngle, double pitchAngle, double drillLength) { //courseAngle 航向角， pitchAngle 俯仰角，drillLength 钻杆长度

  //design_x2 = design_x2 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
  //  design_y2 = design_y2 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
  //  design_z2 = design_z2 + ZGCD * cos(user_data[3].toFloat()*PI/180);

  //  series1->append(design_x2,design_y2);//俯视图
  //  series6->append(design_x2,design_z2);//侧视图
    x_2 = x_2 + drillLength * cos(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    y_2 = y_2 + drillLength * sin(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    z_2 = z_2 + drillLength * cos(pitchAngle*PI/180);
    series5->append(x_2,y_2);
    series7->append(x_2,z_2);
}

//绘制施工曲线点图
void WorkPlatForm::drawLineChart(){

}

WorkPlatForm::~WorkPlatForm()
{
    delete ui;
}

void WorkPlatForm::on_pushButton_2_clicked()
{

    PitchAngle_fy = ui->qj->text().toDouble();
    CourseAngle_fy = ui->fwj->text().toDouble();
    drillLength_1 = ui->zgcd->text().toDouble();
    figureDrillPosition_1(CourseAngle_fy,PitchAngle_fy,drillLength_1);
    series2->remove(1);
    drawGJMPieChart();
    series3->remove(1);
    series4->remove(1);
    drawFwjQjPieChart();

    //记录界面 添加 一条数据
    rf->add_sgqx(ui->fwj->text(),ui->qj->text(),ui->zgcd->text());


}

void WorkPlatForm::read_Excel(){

    //读取钻杆信息
    QString fileName = QApplication::applicationDirPath() + "/conf/InitParam.ini";

    QFile file1;
    file1.setFileName(fileName);
    QStringList  user_data;
    double ZGCD = mutiSignalDecode->sys_zgcd[HIGH];


    ui->zgcd->setText(QString::number(ZGCD,'f',1));
    ui->zgcd->setReadOnly(true);

    //读取设计曲线信息
    fileName = QApplication::applicationDirPath() + "/conf/shejiquxian.txt";
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"打开失败";
    }

    QTextStream in(&file);


    while(!in.atEnd()){
        QString line = in.readLine();
        user_data = line.split("|");

        //design_x2 = design_x2 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
        //  design_y2 = design_y2 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
        //  design_z2 = design_z2 + ZGCD * cos(user_data[3].toFloat()*PI/180);
        x_1 = x_1 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
        y_1 = y_1 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
        z_1 = z_1 + ZGCD * cos(user_data[3].toFloat()*PI/180);
        //qDebug()<<"x_1="<<x_1<<"   y_1"<<y_1<<"  z_1"<<z_1<<endl;
        series1->append(x_1,y_1);//侧视图
        series6->append(x_1,z_1);//俯视图

    }
    file.close();


    //读取施工曲线信息
    fileName = QApplication::applicationDirPath() + "/conf/shigongquxian.txt";
    QFile file2(fileName);
    if(!file2.open(QIODevice::ReadOnly)){
        qDebug()<<"打开失败";
    }

    QTextStream in2(&file2);


    while(!in2.atEnd()){
        QString line = in2.readLine();
        user_data = line.split("|");
        figureDrillPosition_1(user_data[2].toFloat(),user_data[3].toFloat(),ZGCD);

    }
    file2.close();

}

void WorkPlatForm::save_Execel(){

}
